package hxg.thread.lock;

import java.util.Arrays;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author HeXiangGuang
 * @create 2021-06-23 23:12
 * ReentrantLock:Lock接口的实现类，与synchronized一样具有互斥锁功能。
 */
public class MyList {

    private Lock locker = new ReentrantLock();
    private String[] strs = {"A","B","",""};
    private int count = 2;
    //添加元素
    public void add(String value){
        locker.lock();
        try {
            strs[count] = value;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            count ++;
        } finally {
            locker.unlock();
        }
    }

    @Override
    public String toString() {
        return "MyList{" +
                "strs=" + Arrays.toString(strs) +
                '}';
    }

    public String[] getStrs() {
        return strs;
    }
}
